package com.lori.netshop.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lori.netshop.pojo.TbItem;
import com.lori.netshop.service.ItemParamItemService;
import com.lori.netshop.service.ItemService;
import com.lori.netshop.util.IDUtil;
import com.lori.netshop.util.NetshopResult;

@Controller
public class ItemController {
	//依赖注入service
	@Autowired
	private ItemService itemServiceimp;
	@Autowired
	private ItemParamItemService itemParamItemServiceimp;
	@RequestMapping("/test/{id}")
	public @ResponseBody TbItem findTbItem(@PathVariable long id){
		TbItem findTbItem = itemServiceimp.findTbItem(id);
		return findTbItem;
	}
	/**
	 * 功能：获得分页TbItem
	 * 
	 */
	@RequestMapping("/item/list")
	@ResponseBody
	public Map pageItem(int page,int rows){
		Map pageTbItem = itemServiceimp.pageTbItem(page, rows);
		return pageTbItem;
	}
	/**
	 * 功能：保存tbitem和tbitemdesc
	 * 
	 */
	@RequestMapping("/item/save")
	@ResponseBody
	public NetshopResult save(TbItem tbitem,String desc,String itemParams){
		//获得唯一ID
		long id = IDUtil.getID();
		tbitem.setId(id);
		//保存规格参数
		NetshopResult result = itemParamItemServiceimp.save(tbitem.getId(), itemParams);
		if(result != null && result.getStatus() == 200){
			return itemServiceimp.save(tbitem, desc);
		}else{
			return new NetshopResult(200,"保存失败",null);
		}
	}
}
